import sys
import cv2
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import matplotlib.pyplot as plt
import qdarkstyle
from qdarkstyle import LightPalette


from custom.stackedWidget import StackedWidget
from custom.treeView import FileSystemTreeView
from custom.listWidgets import FuncListWidget, UsedListWidget
from custom.graphicsView import GraphicsView
from custom.bottomView import BottomView


#  目前不用
# from custom.resultView import  resultView


##  暂时不用
from custom.cameraView import CameraView

from camera import *

from  config import  selectImage_flag

class MyApp(QMainWindow):
    def __init__(self):
        super(MyApp, self).__init__()

        """
            照相机
        """

        self.cc = Camera()

        #

        self.src_img = None

        #

        self.cur_img = None

        """
            工具栏
        
        """

        #   TODO 工具栏格式调整

        self.tool_bar = self.addToolBar('工具栏')
        self.action_right_rotate = QAction(QIcon("icons/右旋转.png"), "向右旋转90", self)
        self.action_left_rotate = QAction(QIcon("icons/左旋转.png"), "向左旋转90°", self)
        self.action_histogram = QAction(QIcon("icons/直方图.png"), "直方图", self)
        #   用connect绑定事件

        self.action_right_rotate.triggered.connect(self.right_rotate)
        self.action_left_rotate.triggered.connect(self.left_rotate)
        self.action_histogram.triggered.connect(self.histogram)

        self.tool_bar.addActions((self.action_left_rotate, self.action_right_rotate, self.action_histogram))

        """
            引入组件
        """

        # 需要引入照相机
        self.graphicsView = GraphicsView(self, camera=self.cc)

        self.useListWidget = UsedListWidget(self)

        # 需要引入照相机
        self.funcListWidget = FuncListWidget(self,camera=self.cc, graphicsView= self.graphicsView)

        self.stackedWidget = StackedWidget(self)


        # 引入 中间 画布
        self.fileSystemTreeView = FileSystemTreeView(self, graphicsView= self.graphicsView)


        self.bottomView = BottomView(self)




        # # 需要引入照相机
        # self.cameraView = CameraView(self, camera=self.cc)


        """
            主界面 展示 格式
        """

        self.dock_file = QDockWidget(self)
        self.dock_file.setWidget(self.fileSystemTreeView)
        self.dock_file.setTitleBarWidget(QLabel('工作目录'))
        self.dock_file.setFeatures(QDockWidget.NoDockWidgetFeatures)

        self.dock_func = QDockWidget(self)
        self.dock_func.setWidget(self.funcListWidget)
        self.dock_func.setTitleBarWidget(QLabel('图像操作'))
        self.dock_func.setFeatures(QDockWidget.NoDockWidgetFeatures)

        self.dock_used = QDockWidget(self)
        self.dock_used.setWidget(self.useListWidget)
        self.dock_used.setTitleBarWidget(QLabel('已选操作'))
        self.dock_used.setFeatures(QDockWidget.NoDockWidgetFeatures)
        self.dock_used.setFeatures(QDockWidget.NoDockWidgetFeatures)

        self.dock_attr = QDockWidget(self)
        self.dock_attr.setWidget(self.stackedWidget)
        self.dock_attr.setTitleBarWidget(QLabel('属性'))
        self.dock_attr.setFeatures(QDockWidget.NoDockWidgetFeatures)
        # 设置默认 不显示
        self.dock_attr.close()


        self.dock_bottom = QDockWidget(self)
        self.dock_bottom.setWidget(self.bottomView)
        self.dock_bottom.setTitleBarWidget(QLabel('有取向钢结果'))
        self.dock_bottom.setFeatures(QDockWidget.NoDockWidgetFeatures)


        """
            组件摆放位置
        """

        self.setCentralWidget(self.graphicsView)
        # self.setCentralWidget(self.cameraView)



        self.addDockWidget(Qt.LeftDockWidgetArea, self.dock_file)
        self.addDockWidget(Qt.TopDockWidgetArea, self.dock_func)
        self.addDockWidget(Qt.RightDockWidgetArea, self.dock_used)
        self.addDockWidget(Qt.RightDockWidgetArea, self.dock_attr)
        self.addDockWidget(Qt.BottomDockWidgetArea, self.dock_bottom)



        """
            界面
        """

        self.setWindowTitle('Opencv图像处理')
        self.setWindowIcon(QIcon('icons/main.png'))





    # 修改图片

    def update_image(self):
        if self.src_img is None:
            return
        img = self.process_image()
        self.cur_img = img
        self.graphicsView.update_image(img)

    def change_image(self, img):
        self.src_img = img
        img = self.process_image()
        self.cur_img = img
        self.graphicsView.change_image(img)

    def process_image(self):
        img = self.src_img.copy()
        for i in range(self.useListWidget.count()):



            img = self.useListWidget.item(i)(img)

            #img = self.useListWidget.item(i)(img)

        return img

    # 右旋转 实现

    def right_rotate(self):
        self.graphicsView.rotate(90)

    # 左旋转 实现

    def left_rotate(self):
        self.graphicsView.rotate(-90)

    #  直方图

    def histogram(self):
        color = ('b', 'g', 'r')
        for i, col in enumerate(color):
            histr = cv2.calcHist([self.cur_img], [i], None, [256], [0, 256])
            histr = histr.flatten()
            plt.plot(range(256), histr, color=col)
            plt.xlim([0, 256])
        plt.show()


if __name__ == "__main__":

    app = QApplication(sys.argv)

    # 创建一个字体
    font = QFont("NSimSun")
    font.setPointSize(12)
    # 设置全局字体
    app.setFont(font)

    # 样式
    # app.setStyleSheet(open('./custom/styleSheet.qss', encoding='utf-8').read())

    app.setStyleSheet(qdarkstyle.load_stylesheet(qt_api='pyqt5', palette=LightPalette()))

    window = MyApp()

    window.showMaximized()


    window.show()
    sys.exit(app.exec_())
